-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for query parameters #2776
Add support for query parameters #2776
Conversation
Holds name, type, and value for scalar query parameters, and handles marshalling them to / from JSON representation mandated by the BigQuery API. Toward #2551.
Holds name, type, and value for array query parameters, and handles marshalling them to / from JSON representation mandated by the BigQuery API. Toward #2551.
Holds name, types, and values for Struct query parameters, and handles marshalling them to / from JSON representation mandated by the BigQuery API. Toward #2551.
It is just a list comprehension, really.
@@ -28,3 +28,6 @@ | |||
from google.cloud.bigquery.dataset import Dataset | |||
from google.cloud.bigquery.schema import SchemaField | |||
from google.cloud.bigquery.table import Table | |||
from google.cloud.bigquery._helpers import ArrayQueryParameter |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
"""Named / positional query parameters for scalar values. | ||
|
||
:type name: str or None | ||
:param name: Parameter name, used via `@foo` syntax. If None, the |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
@classmethod | ||
def positional(cls, type_, value): | ||
"""Factory for positional paramters. | ||
|
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
:param value: the scalar parameter value. | ||
|
||
:rtype: :class:`ScalarQueryParameter` | ||
:returns: instance w/o name |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
"""Factory for positional paramters. | ||
|
||
:type sub_parms: tuple of :class:`ScalarQueryParameter` | ||
:param sub_parms:s the sub-parameters for the struct |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
|
||
@staticmethod | ||
def _get_target_class(): | ||
from google.cloud.bigquery._helpers import AbstractQueryParameter |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
"""Named / positional query parameters for scalar values. | ||
|
||
:type name: str or None | ||
:param name: Parameter name, used via `@foo` syntax. If None, the |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
class ScalarQueryParameter(object): | ||
"""Named / positional query parameters for scalar values. | ||
|
||
:type name: str or None |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
|
||
:type name: str or None | ||
:param name: Parameter name, used via `@foo` syntax. If None, the | ||
paramter can only be addressed via position (`?`). |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
""" | ||
name = resource.get('name') | ||
type_ = resource['parameterType']['type'] | ||
value = resource['parameterValue']['value'] |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
:class:`datetime.date`. | ||
:param value: the scalar parameter value. | ||
""" | ||
def __init__(self, name, type_, value): |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
query_parameter.to_api_repr() | ||
for query_parameter in self._query_parameters | ||
] | ||
if self._query_parameters[0].name is None: |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
@@ -1287,12 +1287,31 @@ def _verifyIntegerResourceProperties(self, job, config): | |||
else: | |||
self.assertIsNone(job.maximum_bytes_billed) | |||
|
|||
def _verifyUDFResources(self, job, config): |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
def test_begin_w_named_query_parameter(self): | ||
from google.cloud.bigquery._helpers import ScalarQueryParameter | ||
query_parameters = [ScalarQueryParameter('foo', 'INT64', 123)] | ||
PATH = 'projects/%s/jobs' % self.PROJECT |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
if token is None: | ||
break | ||
jobs, token = client.list_jobs(page_token=token) # API request | ||
job_iterator = client.list_jobs() # API request |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
@@ -490,6 +486,30 @@ def client_run_sync_query(client, _): | |||
|
|||
|
|||
@snippet | |||
def client_run_sync_query_w_param(client, _): | |||
"""Run a synchronous query using a query parameter""" | |||
QUERY_W_PARM = ( |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
|
||
:type type_: str | ||
:param type_: name of parameter type. One of `'STRING'`, `'INT64'`, | ||
`'FLOAT64'`, `'BOOLEAN'`, `'TIMESTAMP'`, or `'DATE'`. |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
The back-end raises an exception when passed the 'BOOLEAN' type to define a query parameter. Addresses: #2776 (comment)
{'name': name, 'type': self.struct_types[name]} | ||
for name in self._order | ||
{'name': key, 'type': value} | ||
for key, value in self.struct_types.items() |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
the class). | ||
|
||
:type value: list of instances of classes derived from | ||
:class:`AbstractQueryParameter`. |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, though I don't understand why you'd have the constructor and the API rep so far apart, especially if no human is every going to use the constructor
I do expect humans to use the constructor, e.g.: from google.cloud.bigquery import Client
from google.cloud.bigquery import ScalarQueryParameter
from google.cloud.bigquery import StructQueryParameter
birth_state = ScalarQueryParameter('birth_state', 'STRING', 'VA')
death_state = ScalarQueryParameter('death_state', 'STRING', 'MS')
birth_death = StructQueryParameter(
'birth_death', birth_state, death_state)
client = Client()
query = client.run_sync_query(
'SELECT * FROM mortality WHERE birth_death = @birth_death') |
* Add 'ScalarQueryParameter' class. Holds name, type, and value for scalar query parameters, and handles marshalling them to / from JSON representation mandated by the BigQuery API. * Factor out 'AbstractQueryParameter. * Add 'ArrayQueryParameter' class. Holds name, type, and value for array query parameters, and handles marshalling them to / from JSON representation mandated by the BigQuery API. * Add 'StructQueryParameter' class. Holds name, types, and values for Struct query parameters, and handles marshalling them to / from JSON representation mandated by the BigQuery API. * Add 'QueryParametersProperty' descriptor class. * Add 'query_parameters' property to 'QueryResults' and 'QueryJob'. * Plumb 'udf_resources'/'query_parameters' through client query factories. * Expose concrete query parameter classes as package APIs. Closes googleapis#2551.
does this mean we can use @parameters in the BigQuery UI ?? I cant seem to figure out how to declare them correctly. |
@avitzavi I'm not sure what you are asking. Does the example in the docs help? |
I’m trying to use variables like I do in sql server:
“Declare @variable INT = 10”
Can I do this in the BigQuery UI?
… On Oct 31, 2018, at 2:25 PM, Tres Seaver ***@***.***> wrote:
@avitzavi I'm not sure what you are asking. Does the example in the docs help?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.
|
The Also, I don't know what you mean by "BigQuery UI" -- is that the https://console.cloud.google.com/ interface? |
Yes it is the console.
The Declare/Set methods from SQL are very valuable for me as a data analyst.
Seems like a really big missing feature...
I create many reports (based off scheduled queries) and not having these parameters makes it much more challenging to do.
Especially given the Console's lack of basic editor features like find/replace.
… Oct 31, 2018, at 5:30 PM, Tres Seaver ***@***.***> wrote:
The @parameter placeholders added by this PR are used for passing in values from the client to be substituted into a query: they aren't meant to be used that way.
Also, I don't know what you mean by "BigQuery UI" -- is that the https://console.cloud.google.com/ interface?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.
|
@avitzavi This repository is solely about the Python client libraries which wrap the various Google Cloud APIs. Please check out the BigQuery support page for ways to get support for the back-end API itself. |
Closes #2551.